package com.CityHunter.MapActivity;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.CityHunter.FriendList;
import com.CityHunter.R;
import com.CityHunter.Track;
import com.CityHunter.Activity.CameraView;
import com.CityHunter.OverLay.FriendOl;
import com.CityHunter.OverLay.MapOl;
import com.CityHunter.OverLay.TrackOverLay;
import com.CityHunter.SQL_Lite.DB_Helper;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class RecordTrack extends MapActivity {
	
	GeoPoint gp;
	MapView mview;
	MapController mc;
	LocationManager lm;
	List<Overlay> mapOverlays;
	
	Location loc;
	FriendOl fol;
	SharedPreferences sp;
	MapOl mo;
	String grouptitle;
	FriendList fl = new FriendList();
	Track track;
	 SQLiteDatabase db;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.map);
		
		String serviceName = Context.LOCATION_SERVICE;
		lm = (LocationManager) getSystemService(serviceName);
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		String provider = lm.getBestProvider(criteria, true);
		loc = lm.getLastKnownLocation(provider);
		mview = (MapView) findViewById(R.id.mapview);
		mc = mview.getController();
		mc.setZoom(15);
		mview.setBuiltInZoomControls(true);
		mapOverlays = mview.getOverlays();
		DB_Helper dbHelper= new DB_Helper(this,"CitHunter");
		  SQLiteDatabase db=dbHelper.getWritableDatabase();
		  track=new Track("Testing","");
		
		centerOnGPSPosition();

	//	 lm.requestLocationUpdates(provider, 0, 0, locationListener);


	}
	public boolean onCreateOptionsMenu(Menu menu) {
	       
        menu.add(0, 0, 0, "Realtity View");
      menu.add(0, 1, 1, "Exit");
    return super.onCreateOptionsMenu((android.view.Menu) menu);
   }
     
    public boolean onOptionsItemSelected(MenuItem item) {
 
       switch(item.getItemId()) {
           case 0:
             
            case 1:
             
                break;
            default:
        }
    return super.onOptionsItemSelected(item);
    }
	public void centerOnGPSPosition() {

		gp = new GeoPoint((int) (loc.getLatitude() * 1000000), (int) (loc
				.getLongitude() * 1000000));

		mc.animateTo(gp);
		mc.setCenter(gp);
	
		mo = new MapOl(gp);
	
	

		mapOverlays.add(mo);
		
		
		// mapOverlays.add(new TrackOverLay(track));
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void newPoint(Location loc){
		track.addPointByLatLong(loc.getLatitude(), loc.getLongitude());
		 ContentValues cv=new ContentValues();
		 cv.put("trackname", track.getName());
		 cv.put("user"," Aaron");
		 cv.put("lat", loc.getLatitude());
		 cv.put("long", loc.getLongitude());
		 db.insert("track", null, cv);
		 
	}

	
	private final LocationListener locationListener = new LocationListener() {
		public void onLocationChanged(Location location) {

			gp = new GeoPoint((int) (location.getLatitude() * 1000000),
					(int) (location.getLongitude() * 1000000));
			mc.animateTo(gp);
			mc.setCenter(gp);
			mapOverlays.clear();
			mapOverlays.add(mo);

		newPoint(location);
		

			
			 mapOverlays.add(new TrackOverLay(track));
		}

		public void onProviderDisabled(String provider) {

		}

		public void onProviderEnabled(String provider) {
		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	};
}
